package com.howie.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.howie.gulimall.product.entity.ProductAttrValueEntity;
import com.howie.gulimall.product.service.ProductAttrValueService;
import com.howie.gulimall.product.vo.AttrResponseVo;
import com.howie.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.howie.gulimall.product.service.AttrService;
import com.howie.common.utils.PageUtils;
import com.howie.common.utils.R;



/**
 * 商品属性
 *
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2020-12-06 19:27:40
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 修改商品规格
     * 请求路径：/product/attr/update/{spuId}
     */
    @PostMapping("/update/{spuId}")
    public R updateAttrValueForSpu(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateAttrValueForSpu(spuId,entities);
        return R.ok();
    }

    /**
     * 获取spu规格
     * 请求路径：/product/attr/base/listforspu/{spuId}
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.getListForSpu(spuId);
        return R.ok().put("data",productAttrValueEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 规格参数请求路径 /product/attr/base/list/{catelogId}
     * 销售属性请求路径 /product/attr/sale/list/{catelogId}
     * 获取分类规格参数与销售属性
     * 注解@RequestParam 将请求参数绑定到你控制器的方法参数上（是SpringMVC中接收普通参数的注解）
     */
    @RequestMapping(path = "/{attrType}/list/{catelogId}",method = RequestMethod.GET)
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable Long catelogId,
                          @PathVariable String attrType){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);
        return R.ok().put("page",page);
    }

    /**
     * 根据属性id获取属性及关联的信息
     * 请求路径：/product/attr/info/{attrId}
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		// AttrEntity attr = attrService.getById(attrId);
        AttrResponseVo attrResponseVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
